import { TreeDataProvider, commands, Event, window, TreeItem, TreeItemCollapsibleState, ProviderResult } from "vscode";
const path = require("path");
const tools = require("./util.js");

export class DataProvider implements TreeDataProvider<DataItem> {
  onDidChangeTreeData?: Event<DataItem | null | undefined> | undefined;
  data: DataItem[];

  constructor() {
    this.data = [
      new DataItem('安装依赖'),
      new DataItem('启动开发环境'),
      new DataItem('构建生产环境')
    ];
  }

  getTreeItem(element: DataItem): TreeItem | Thenable<TreeItem> {
    return element;
  }

  getChildren(element?: DataItem | undefined): ProviderResult<DataItem[]> {
    if (element === undefined) {
      return this.data;
    }
    return element.children;
  }
}


class DataItem extends TreeItem {
  public children: DataItem[] | undefined;

  constructor(label: string, children?: DataItem[] | undefined) {
    super(label, children === undefined ? TreeItemCollapsibleState.None : TreeItemCollapsibleState.Collapsed);
    this.children = children;
  }

  iconChoose(){
   let icon = null;
   switch (this.label) {
     case "安装依赖":
       icon="install.svg";
       break;
    case "启动开发环境":
       icon="dev.svg";
      break;
    case "构建生产环境":
      icon="pro.svg";
      break;
     default:
       break;
   }
   return icon;
  }

  iconPath = {
		light: path.join(__filename, '..', '..','images', this.iconChoose()),
		dark: path.join(__filename, '..','..', 'images', this.iconChoose())
	};
}

module.exports = function (context: any) {


  const provider = new DataProvider();
  // 数据注册
  window.registerTreeDataProvider('tele-hot', provider);


  context.subscriptions.push(commands.registerCommand('tele-hot.editEntry', (item: DataItem) => {
    switch (item['label']) {
      case "安装依赖":
        tools.runProject({
          name:"安装依赖",
          cmd:"npm  install"
        });
        break;
      case "启动开发环境":
        tools.runProject({
          name:"启动开发环境",
          cmd:"npm run serve"
        });
        break;
      case "构建生产环境":
        tools.runProject({
          name:"构建生产环境",
          cmd:"npm run build"
        });
        break;
      default:
        break;
    }

  }));

};